﻿/*  ----------------------------------------------------------------------------
 *  SysServer
 *  ----------------------------------------------------------------------------
 *  File:       SocketAsyncEventArgsPool.cs
 *  Creater:     SEEN\MistSeen
 *  ----------------------------------------------------------------------------
 *  $Date: 2010-09-06 10:36:01 +0000 (Mon, 06 Sep 2010) $
 *  $Rev: 15 $
 *  $Author: ScorDeity $
 *  $URL: http://sysserver.googlecode.com/svn/trunk/src/SysServer/Sock/SocketAsyncEventArgsPool.cs $
 *  $Id: SocketAsyncEventArgsPool.cs 15 2010-09-06 10:36:01Z ScorDeity $
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace SysServer.Sock
{
    using System.Net.Sockets;
    public sealed class SocketAsyncEventArgsPool
    {
        private object LockForTokenId = new object();
        private int _NextTokenId =  1;
        private Stack<SocketAsyncEventArgs> _ArgsPool;

        public SocketAsyncEventArgsPool(int capacity)
        {
            this._ArgsPool = new Stack<SocketAsyncEventArgs>(capacity);
        }

        public int Count
        {
            get { return this._ArgsPool.Count; }
        }

        public SocketAsyncEventArgs Pop()
        {
            lock (this._ArgsPool)
            {
                return this._ArgsPool.Pop();
            }
        }

        public void Push(SocketAsyncEventArgs item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("Item of SocketAsyncEventArgs  added to  can not null");               
            }
            lock (this._ArgsPool)
            {
                this._ArgsPool.Push(item);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public int GetTokenId()
        {
            int tokenId = 0;
            lock (this.LockForTokenId)
            {
                tokenId = _NextTokenId;
                _NextTokenId++;
            }
            return tokenId;
        }
    }
}